package bluetooth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.Notification.Action;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("NewApi")
public class NotificationMonitor extends NotificationListenerService {
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Bundle mBundle = ((Bundle) msg.obj);
//                    Intent intent2 = new Intent(BluetoothLeService.ACTION_NOTIFICATION);
//                    intent2.putExtra(BluetoothLeService.ACTION_NOTIFICATION, mBundle);
//                    sendBroadcast(intent2);
                    String appPackageName=mBundle.getString("appPackageName");
                    String appNotificationTitle=mBundle.getString("appNotificationTitle");
                    String appNotificationText=mBundle.getString("appNotificationText");
				    Toast.makeText(NotificationMonitor.this, "onNotificationPosted\n"+appPackageName+"\n"+appNotificationTitle+"\n"+appNotificationText+"\n", Toast.LENGTH_SHORT).show();

                    break;
                case 1:
//				Toast.makeText(NotificationMonitor.this, "onNotificationRemoved\n"+msg.obj, Toast.LENGTH_SHORT).show();

                    break;

                default:
                    break;
            }

        }

        ;
    };

    @Override
    public void onCreate() {
//		super.onCreate();
        Toast.makeText(this, "onCreate", 1).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // a.equals("b");
        Toast.makeText(this, "onBind", 1).show();
        return super.onBind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {


        Bundle extras = sbn.getNotification().extras;
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        String notificationSubText = extras.getString(Notification.EXTRA_SUB_TEXT);
        String notificationPkg = sbn.getPackageName();

        Message msg = new Message();
        msg.what = 0;
        Bundle bundle = new Bundle();
        bundle.putString("appPackageName", notificationPkg.toString());
        bundle.putString("appNotificationTitle", notificationTitle.toString());
        if (notificationText == null) {
            bundle.putString("appNotificationText", notificationText.toString());

        } else {
            bundle.putString("appNotificationText", "未知");

        }
        msg.obj = bundle;
        handler.sendMessage(msg);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {


        Bundle extras = sbn.getNotification().extras;
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        String notificationSubText = extras.getString(Notification.EXTRA_SUB_TEXT);

        Message msg = new Message();
        msg.what = 1;
        Bundle bundle = new Bundle();
        bundle.putString("appPackageName", sbn.getPackageName().toString());
        bundle.putString("appNotificationTitle", sbn.getPackageName().toString());
        bundle.putString("appNotificationText", notificationText.toString());
        msg.obj = bundle;
        handler.sendMessage(msg);

    }

}
